import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/admin/home'
  },
  {
    path: '/admin',
    component: () => import('@/Admin.vue'),
    meta: { is_footer: true, is_header: true },
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home/index.vue'),
        meta: { is_footer: true, is_header: true },
        name: '主页'
      },
      {
        path: 'login',
        component: () => import('@/views/Login/index.vue'),
        meta: { is_footer: false, is_header: false },
        name: '登录'

      },
      {
        path: 'classification',
        component: () => import('@/views/ClassiFication/index.vue'),
        meta: { is_footer: true, is_header: true },
        name: '分类'
      },
      {
        path: 'my',
        component: () => import('@/views/My/index.vue'),
        meta: { is_footer: true, is_header: false },
        name: '我的'
      },
      {
        path: 'subject',
        component: () => import('@/views/Subject/index.vue'),
        meta: { is_footer: true, is_header: false },
        name: '专题'
      },
      {
        path: 'shopping',
        component: () => import('@/views/Shopping/index.vue'),
        meta: { is_footer: true, is_header: false },
        name: '购物车'
      },
      {
        path: 'search',
        component: () => import('@/views/Search/index.vue'),
        meta: { is_footer: false, is_header: false },
        name: '搜索'
      },
      {
        path: 'category',
        component: () => import('@/views/Category/index.vue'),
        meta: { is_footer: true, is_header: true },
        name: '分类详情'
      },
      {
        path: 'details',
        component: () => import('@/views/details/index.vue'),
        meta: { is_footer: false, is_header: false }
      }
    ]
  },
  {
    path: '*',
    component: () => import('@/404.vue')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path == '/admin/shopping') {
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      Toast('请先登录哦')
      setTimeout(() => {
        router.push('/admin/my')
      }, 1000)
    }
  }
  const title = (to.name ? to.name : '购物叭！')
  document.title = title
  next()
})

export default router
